package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.entity.DdShop;
import com.dd.entity.DdTop;
import com.dd.entity.DdUser;
import com.dd.entity.DdUserpage;
import com.dd.util.PageList;

public class AppShopAction extends BaseAction {
	private  double EARTH_RADIUS = 6378.137;

	private  double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public  double GetDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		// s = Math.round(s * 10000) / 10000;
		return s * 1000;
	}

	public ActionForward toprecommend(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		StringBuffer sbf = new StringBuffer("{\"total\":");
		try {
			int skip = 0;
			int count = 10;
			if (request.getParameter("skip") != null) {
				skip = Integer.parseInt(request.getParameter("skip"));
			}
			if (request.getParameter("count") != null) {
				count = Integer.parseInt(request.getParameter("count"));
			}

			PageList list = this.getDdShopDAO().findTops(skip, count);
			sbf.append(list.getTotalCount() + ",\"tops\":[");
			for (int i = 0; i < list.size(); i++) {
				DdTop dt = (DdTop) list.get(i);
				if (i == 0) {
					sbf.append("{\"type\":\"" + dt.getTypes() + "\",\"sid\":\""
							+ dt.getSid() + "\",\"content\":\""
							+ dt.getContent() + "\",\"imgurl\":\""
							+ dt.getImgurl() + "\",\"href\":\"" + dt.getHref()
							+ "\"}");
				} else {
					sbf.append(",{\"type\":\"" + dt.getTypes()
							+ "\",\"sid\":\"" + dt.getSid()
							+ "\",\"content\":\"" + dt.getContent()
							+ "\",\"imgurl\":\"" + dt.getImgurl()
							+ "\",\"href\":\"" + dt.getHref() + "\"}");
				}
			}
			sbf.append("]}");
			out.println(sbf.toString());
			out.flush();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward topsearch(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		StringBuffer sbf = new StringBuffer("{\"total\":");
		try {
			String q = request.getParameter("q");
			double x = Double.parseDouble(request.getParameter("x"));
			double y = Double.parseDouble(request.getParameter("y"));

			List list = this.getDdShopDAO().findByName(q);
			sbf.append(list.size() + ",\"result\":[");
			for (int i = 0; i < list.size(); i++) {
				DdShop ds = (DdShop) list.get(i);
				String content = "";
				if (ds.getIssend().intValue() == 1) {
					content = ds.getSendreq();
				} else {
					content = ds.getBookreq();
				}
				if (i == 0) {
					sbf.append("{\"sid\":\""
							+ ds.getId()
							+ "\",\"name\":\""
							+ ds.getName()
							+ "\",\"address\":\""
							+ ds.getAddress()
							+ "\",\"issend\":\""
							+ ds.getIssend()
							+ "\",\"isbook\":\""
							+ ds.getIsbooking()
							+ "\",\"iscoupon\":\""
							+ ds.getIscoupon()
							+ "\",\"content\":\""
							+ content
							+ "\",\"distance\":\""
							+ (int) this.GetDistance(Double.parseDouble(ds
									.getPosx_g()), Double.parseDouble(ds
									.getPosy_g()), x, y) + "\"}");
				} else {
					sbf.append(",{\"sid\":\""
							+ ds.getId()
							+ "\",\"name\":\""
							+ ds.getName()
							+ "\",\"address\":\""
							+ ds.getAddress()
							+ "\",\"issend\":\""
							+ ds.getIssend()
							+ "\",\"isbook\":\""
							+ ds.getIsbooking()
							+ "\",\"iscoupon\":\""
							+ ds.getIscoupon()
							+ "\",\"content\":\""
							+ content
							+ "\",\"distance\":\""
							+ (int) this.GetDistance(Double.parseDouble(ds
									.getPosx_g()), Double.parseDouble(ds
									.getPosy_g()), x, y) + "\"}");

				}
			}
			sbf.append("]}");
			out.println(sbf.toString());
			out.flush();
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward shopinfo(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		StringBuffer sbf = new StringBuffer("");
		PrintWriter out = response.getWriter();
		try {
			HttpSession session = request.getSession();
			int uid = 0;
			if (session.getAttribute("logphone") != null) {
				String phone = (String) session.getAttribute("logphone");
				DdUser du = this.getDdUserDAO().findByPhone(phone);
				uid = du.getId();
			}

			int sid = Integer.parseInt(request.getParameter("sid"));
			DdShop ds = this.getDdShopDAO().findById(sid);
			DdUserpage dup = this.getDdUserpageDAO().findByUserAndShopid(uid,
					sid);
			int fav = 0;
			if (dup != null) {
				fav = 1;
			}
			String cost = "0-50";
			if (ds.getCost() != null && ds.getCost().intValue() == 2) {
				cost = "50-100";
			} else if (ds.getCost() != null && ds.getCost().intValue() == 3) {
				cost = "100-200";
			} else if (ds.getCost() != null && ds.getCost().intValue() == 4) {
				cost = "200以上";
			}
			String menustr = "";
			sbf.append("{\"fav\":\""
					+ fav
					+ "\",\"name\":\""
					+ ds.getName()
					+ "\",\"address\":\""
					+ ds.getAddress()
					+ "\",\"phone\":\""
					+ ds.getPhone()
					+ "\",\"x\":\""
					+ ds.getPosx_g()
					+ "\",\"y\":\""
					+ ds.getPosy_g()
					+ "\",\"wifi\":\""
					+ ds.getWifi()
					+ "\",\"park\":\""
					+ (ds.getPark().intValue() == -1 ? 0 : 1)
					+ "\",\"vias\":\""
					+ ds.getVisa()
					+ "\",\"style\":\""
					+ (ds.getStylevalue().length() == 0 ? "外卖送餐" : ds
							.getStylevalue()) + "\",\"shophours\":\""
					+ ds.getShophours() + "\",\"cost\":\"" + cost
					+ "\",\"isbook\":\"" + ds.getIsbooking()
					+ "\",\"bookreq\":\"" + ds.getBookreq()
					+ "\",\"issend\":\"" + ds.getIssend()
					+ "\",\"sendtype\":\""
					+ (ds.getSendtype() == 1 ? "外送" : "自取")
					+ "\",\"sendreq\":\"" + ds.getSendreq()
					+ "\",\"senddis\":\"" + ds.getSendrange()
					+ "\",\"sendhours\":\"" + ds.getSendtime()
					+ "\",\"iscoupon\":\"" + ds.getIscoupon()
					+ "\",\"coupon\":\"" + ds.getCoupon3() + "\",\"menus\":["
					+ menustr + "]}");
			// ”coupon”:”全场8.8折(酒水夜宵菜品除外)”,”menus”:[{“menu”:”套餐类:”,”price”:””},{“menu”:”套餐A(肉末茄子)”,”price”:”10”},{“menu”:”套餐B(鱼香茄子):”,”price”:”9”},{“menu”:”主食:”,”price”:””},{“menu”:”炒米线”,”price”:”10”},{“menu”:”炒米粉”,”price”:”15”}]}
			ds.setHot(ds.getHot() + 1);
			this.getDdShopDAO().saveOrUpdate(ds);
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out.print("{\"result\":0,\"msg\":\"服务器忙,请稍后再试!\"}");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
}
